from django.test import TestCase, Client
from django.urls import reverse

from users.models import User


# Create your tests here.
class UserModelTests(TestCase):
    """用户-模型 测试类"""

    def setUp(self):
        # 创建测试用户, 用于以后测试
        self.test_user = User.objects.create_user(
            username="lidan",
            password="123456",
            mobile="18888888888",
            email="666@qq.com"
        )

    def test_user_creation(self):
        """测试用户创建"""

        # 验证用户是是否被正确创建. 断言
        self.assertTrue(isinstance(self.test_user, User))
        # 验证用户手机号和邮箱是否正确
        self.assertEqual(self.test_user.mobile,"18888888889")
        self.assertEqual(self.test_user.email,"666@qq.com")

    def test_user_mobile_unique(self):
        """测试用户手机号唯一"""
        User.objects.create_user(
            username="dandan",
            password="1234567",
            mobile="18888888866",
            email="999@qq.com"
        )


class UserLoginTests(TestCase):
    """用户登录测试类"""

    def setUp(self):
        #创建测试用户
        self.user = User.objects.create_user(
            username="dandan",
            password="1234567",
            mobile="18888888866",
            email="999@qq.com"
        )
        #创建测试客户端
        self.client = Client()
        #获取登录url
        self.login_url = reverse("login")



    def test_login_success(self):
        #构建参数
        data = {
            "username":"dandan",
            "password":"1234567"
        }

        # 发送请求
        res = self.client.post(self.login_url,data,format="json")
        print(res.json())

        self.assertEqual(res.data['code'],2)